package com.samehope.ar.service.impl;

import com.samehope.ar.constant.DelFlagEnum;
import com.samehope.ar.constant.IsManageEnum;
import com.samehope.ar.constant.RoleNameEnum;
import com.samehope.ar.dto.CompanyAddParam;
import com.samehope.ar.dto.CompanyUpdateParam;
import com.samehope.ar.entity.*;
import com.samehope.ar.mapper.CompanyMapper;
import com.samehope.ar.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.samehope.ar.util.SessionUtil;
import com.samehope.ar.vo.CompanyDataVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 公司 服务实现类
 * </p>
 *
 * @author Zhang Luo
 * @since 2019-12-23
 */
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements ICompanyService {

    @Autowired
    private ISysUserCompanyService userCompanyService;

    @Autowired
    private ISysUserRoleService userRoleService;

    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private ISysUserService userService;

    /**
     * 新增企业并返回企业ID
     * 1. 新增企业
     * 2. 为用户新增企业管理员角色
     * @param param
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Long addCompany(CompanyAddParam param) {

        final LocalDateTime now = LocalDateTime.now();

        Company company = this.baseMapper.findByCompanyName(param.getCompanyName(), null);

        if (company != null) {
            throw new IllegalArgumentException("企业名称已存在");
        }

        // 保存企业
        company = new Company();
        company.setCompanyName(param.getCompanyName());
        company.setCompanyPic(param.getCompanyPic());
        company.setCreateBy(SessionUtil.getUserId());
        company.setCreateDate(now);
        this.save(company);

        // 创建用户企业关联
        SysUserCompany userCompany = new SysUserCompany();
        userCompany.setCompanyId(company.getId());
        userCompany.setUserId(SessionUtil.getUserId());
        userCompany.setIsManage(IsManageEnum.YES.getCode());
        userCompany.setCreateBy(SessionUtil.getUserId());
        userCompany.setCreateDate(now);
        userCompanyService.save(userCompany);

        // 用户角色创建
        SysRole companyRole = roleService.findByRoleName(RoleNameEnum.COMPANY.getName());
        SysRole employeeRole = roleService.findByRoleName(RoleNameEnum.EMPLOYEE.getName());

        List<SysUserRole> roles = new ArrayList<>();

        SysUserRole var1 = new SysUserRole();
        var1.setCompanyId(company.getId());
        var1.setUserId(SessionUtil.getUserId());
        var1.setRoleId(companyRole.getId());
        var1.setCreateBy(SessionUtil.getUserId());
        var1.setCreateDate(now);
        roles.add(var1);

        SysUserRole var2 = new SysUserRole();
        var2.setCompanyId(company.getId());
        var2.setUserId(SessionUtil.getUserId());
        var2.setRoleId(employeeRole.getId());
        var2.setCreateBy(SessionUtil.getUserId());
        var2.setCreateDate(now);
        roles.add(var2);

        userRoleService.saveBatch(roles);

        // 如果没有默认企业, 第一个企业作为默认企业
        SysUser sysUser = userService.getById(SessionUtil.getUserId());
        if (sysUser.getCompanyId() == null) {
            sysUser.setCompanyId(company.getId());
            userService.updateById(sysUser);
        }

        return company.getId();
    }

    /**
     * 为用户设置默认企业
     * @param companyId
     */
    @Override
    public void setDefault(Long companyId) {

        SysUserCompany userCompany = userCompanyService.findByCompanyAndUser(companyId, SessionUtil.getUserId());
        if (userCompany == null) {
            throw new IllegalArgumentException("您和该企业无从属关系");
        }

        // 如果没有默认企业, 第一个企业作为默认企业
        SysUser sysUser = userService.getById(SessionUtil.getUserId());
        sysUser.setCompanyId(companyId);
        userService.updateById(sysUser);

    }

    @Override
    public void update(CompanyUpdateParam param){
        Company entity = this.getById(param.getCompanyId());
        if(entity == null){
            throw new IllegalArgumentException("企业不存在");
        }

        Company company = this.baseMapper.findByCompanyName(param.getCompanyName(), param.getCompanyId());

        if (company != null) {
            throw new IllegalArgumentException("企业名称已存在");
        }

        // 保存企业
        company = new Company();
        company.setId(param.getCompanyId());
        company.setCompanyPic(param.getCompanyPic());
        company.setCompanyName(param.getCompanyName());
        company.setUpdateBy(SessionUtil.getUserId());
        this.updateById(company);
    }

    @Override
    public void del(Long companyId){
        Company company = new Company();
        company.setId(companyId);
        company.setDelFlag(DelFlagEnum.DELETED.getCode());
        this.updateById(company);
    }

    @Override
    public Company detail(Long companyId){
        return this.baseMapper.selectById(companyId);
    }

    @Override
    public List<CompanyDataVO> findByUser(){
        return this.baseMapper.findByUser(SessionUtil.getUserId());
    }
}
